// pages/AddClassify/AddClassify.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    classify_name: '',
    isOpen: false,
    judgeState: true,
    classify_id: '',
    store_id: '',
    level: '',
    display_order:"",
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.classify_id) {
      this.setData({
        classify_id: options.classify_id
      });
    }
    if (options.store_id) {
      this.setData({
        store_id: options.store_id
      });
    }
    if (options.level) {
      this.setData({
        level: options.level
      });
    }
  },
  onInput(e){
    this.setData({display_order:e.detail.value*1})
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  //确定添加
  sureAdd() {
    let isEnabled = '';
    if (this.data.isOpen) {
      isEnabled = 1;
    } else{
      isEnabled = 0;
    }
    let that = this;
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods-category.add-category');
    let json = {};
    that.judge();
    if (that.data.judgeState == true) {
      if (that.data.classify_id == 'add') {
        json = {
          store_id: that.data.store_id,
          name: that.data.classify_name,
          enabled: isEnabled,
          level: that.data.level,
        };
      } else {
        json = {
          store_id: that.data.store_id,
          name: that.data.classify_name,
          enabled: isEnabled,
          parent_id: that.data.classify_id,
          level: that.data.level,
        };
      }
      json.display_order = that.data.display_order;
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: json,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            wx.showToast({
              title: '添加成功',
              icon: 'none',
              duration: 1500,
              success: function() {
                setTimeout(function() {
                  //要延时执行的代码
                  wx.navigateTo({
                    url: '/packageC/o2o/GoodsClassify/GoodsClassify?store_id=' + that.data.store_id
                  });
                }, 1500); //延迟时间
              }
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }

        },
        fail: function(res) {}
      });
    } else {
      wx.showToast({
        icon: 'none',
        title: '分类信息不完整',
        duration: 1500
      });
    }
  },
  judge() {
    this.setData({
      judgeState: true
    });
    if (this.data.classify_name == '') {
      this.setData({
        judgeState: false
      });
    }
  },
  classifyNameinp(e) {
    let val = e.detail.value;
    this.setData({
      classify_name: val
    });
  },
  isOpenChange(e) {
    let val = e.detail;
    this.setData({
      isOpen: val
    });
  }
});
